<%@ include file="../import_page.jsp"%>
<%@ page import="org.json.*"%>
<%

	//login checking
	if(!loginedChecking(session, UsersType.student)) {
		//user don't login
		//redirect to home page
		response.sendRedirect(URL_HOMEPAGE);
		return;
	}
	
	//select unit check
	if(!selectUnitCheck(session)) {
		//if user didn't select an Unit
		//redirtect to select page
		response.sendRedirect(URL_SELECT_UNIT);
		return;
	}
	
	Units selectUnit = (Units)session.getAttribute(SESSION_SELECT_UNIT);
	String errMsg = null;
	AssignmentCollector ac = null;
	
	//try to catch Assignment ID input
	String acIdStr = request.getParameter(PAR_ASSIGNMENT_ID);
	AssignmentsControl aCon = new AssignmentsControl();
	try {
		int acId = Integer.parseInt(acIdStr);
		
		ac = aCon.getAssignment(acId);
		aCon.closeDBConnection();
		//Check the unit of assignment is match the selected unit
		if(ac.getUnitID().indexOf(selectUnit.getId()) < 0)
			throw new Exception("Select Unit do not match the Assignment collector Unit");
		
	} catch (Exception err) {
		aCon.closeDBConnection();
		response.sendRedirect(URL_VIEW_ASSIGNMENT);
		return;
	}
	
	//catch marking scheme of assignment collector
	String jArrText = "null";
	try {
		//get Marking scheme
		ResultControl rCon = new ResultControl();
		ArrayList<MarkingScheme> listMs = rCon.getMarkingScheme(ac.getId());
		rCon.closeDBConnection();
		//convert to json string
		jArrText = (new JSONArray(listMs)).toString();
	} catch (Exception err) {
		errMsg = err.toString();
	}

%>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Confirm modify</title>
<script type="text/javascript" src="<%= URL_JQUERY %>"></script>
<script type="text/javascript">
//input marking scheme
var arrayMs = <%=jArrText %>;

//run at page load
jQuery(document).ready(function() {
	//if no data in arrayMs, skip the process
	if(arrayMs == null || arrayMs.length < 1)
		return;
	
	//draw to table
	var msTable = jQuery("table#ms_table");
	//clear the old data
	var intr = msTable.children('tbody').children('tr');
	if (intr.length > 1)
		intr.next().remove();
		
	//draw data
	for(var i = 0; i < arrayMs.length; i++) {
		var aRow = jQuery("<tr/>").appendTo(msTable);
		var ltd = jQuery("<td/>").appendTo(aRow);
		var rtd = jQuery("<td/>").appendTo(aRow);
		
		//inner table @ left td
		var innerTb = jQuery("<table width='100%' />").appendTo(ltd);
		//order
		var innRow = jQuery("<tr />").appendTo(innerTb);
		jQuery("<td>Order:</td>").appendTo(innRow);
		var inntd = jQuery("<td>"+ arrayMs[i].order+ "</td>").appendTo(innRow);
		//name
		innRow = jQuery("<tr />").appendTo(innerTb);
		jQuery("<td>Name:</td>").appendTo(innRow);
		inntd = jQuery("<td>"+ arrayMs[i].name+ "</td>").appendTo(innRow);
		//criteria
		innRow = jQuery("<tr />").appendTo(innerTb);
		jQuery("<td>Criteria:</td>").appendTo(innRow);
		inntd = jQuery("<td />").appendTo(innRow);
		//textarea for criteria
		var innTarea = jQuery("<textarea rows=\"5\" cols=\"40\" readonly=\"readonly\"/>");
		innTarea.val(arrayMs[i].criteria);
		innTarea.appendTo(inntd);
		//mark
		jQuery("<span>"+ arrayMs[i].mark+ "</span>").appendTo(rtd);
	}
});
</script>
</head>
<body>
	<%if(!isEmptyString(errMsg)) { %>
	<p><%=errMsg %></p>
	<%} %>
	<table border="1">
		<tr>
			<td>Unit (ID)</td>
			<td><%= selectUnit.getName() %> (<%=selectUnit.getId() %>)</td>
		</tr>
		<tr>
			<td>Assignment Name (ID)</td>
			<td><%= ac.getName() %> (<%=ac.getId() %>)</td>
		</tr>
		<tr>
			<td>Mark</td>
			<td><%=ac.getFullMark() %></td>
		</tr>
	</table>
	<hr />
	<table id="ms_table" border="1">
		<tr>
			<th>Criteria</th>
			<th>Mark</th>
		</tr>
		<!-- arrayMs information in here -->
	</table>
</body>
</html>